$().ready(function(){
    //获取今日收益
    function getDay(){
        let da = new Date();
        let year = da.getFullYear();
        let month = da.getMonth()+1>9?da.getMonth()+1:'0'+(da.getMonth()+1);
        let day = da.getDate()>9?da.getDate():'0'+da.getDate();
        return `${year}-${month}-${day}`

    }
    let today = getDay();
    function getOneProfit(date){
        getAjax(`${global.apiUrl}/admin_community_service_shop_get_day_sy`,res=>{
            if(res){
                console.log(res);

            }else{
                console.log(111);
            }
            $('#today_profit').text(res);

        },{
            date:date,
            service_shop_id:global.community_service_shop_id
        })
    }
    getOneProfit(today);




    //获取时间段收益
    function getAllProfit(beginTimestamp,endTimeStamp){
        getAjax(`${global.apiUrl}/admin_community_service_shop_get_all_sy`,res=>{
            // console.log(res);
            $('#all_profit').text(res);
        },{
            start_time:beginTimestamp,
            end_time:endTimeStamp,
            service_shop_id:global.community_service_shop_id
        })
    }
    getAllProfit();
    //获取当下时间
    function getNow(){
        let date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth()+1;
            month = month>9?month:'0'+month;
        let day = date.getDate();
            day = day>9?day:'0'+day;
        let h = date.getHours();
        h = h>9?h:'0'+h;
        let m = date.getMinutes();
        m = m>9?m:'0'+m;
        let s = date.getSeconds();
        s= s>9?s:'0'+s;
        return `${year}-${month}-${day} ${h}:${m}:${s}`
    };
    let now = getNow();
    let profit_date = [];
    let profit_val = [];
    let profit_start_date = setMonthFirstDay();
    let profit_end_date = now;

    laydate({
        elem:'#start_time',
        event:'click',
        format:'YYYY-MM-DD hh:mm:ss',
        istime:true,
        istoday:true,
        issure:true,
        min:'2010-01-01 00:00:00',
        max:today+' 23:59:59',
        start:now,
        choose:function(dates){
            console.log(dates);
            profit_start_date = dates;
        }
    });
    laydate({
        elem:'#end_time',
        event:'click',
        format:'YYYY-MM-DD hh:mm:ss',
        istime:true,
        istoday:true,
        issure:true,
        min:'2010-01-01 00:00:00',
        max:today+' 23:59:59',
        start:now,
        choose:function(dates){
            console.log(dates);
            profit_end_date = dates;
        }
    });
    $('#select-btn').on('click',function(){
        if(profit_start_date&&profit_end_date){
            getProfitLineChart(profit_start_date,profit_end_date);
        }else{
            alert('请选择日期区间！');
        }
    });
    function getProfitLineChart(start,end){
        let start_time = dateTotimestamp(start);
        let end_time = dateTotimestamp(end);
        getAsyncAjax(`${global.apiUrl}/get_service_shop_sy_picture`,res=>{
            profit_val = [];
            profit_date = [];
            let newArr = setNewList(res);
            newArr = newArr.reverse();
            if(newArr.length == 0){
                $('#profit_charts').css('display','none');
                $('#no-profit').css('display','block');
            }else{
                $('#profit_charts').css('display','block');
                $('#no-profit').css('display','none');
            }
            for(let i in newArr){
                profit_date.push(newArr[i].date);
                profit_val.push(newArr[i].total_price);
            }
            lineChart(profit_date,profit_val);
        },{
            shop_id:global.community_service_shop_id,
            start_time:start_time,
            end_time:end_time
        })
    }
    function setNewList(res){
        let arr = [];
        for(let i=0;i<res.length;i++){
            let date = res[i].date;
            let total = Number(res[i].price);
            for(let j=i+1;j<res.length;j++){
                console.log(res);
                if(res[i].date == res[j].date){
                    total+= Number(res[j].price);
                    res.splice(j,1);
                    j--;
                }
            }
            let profit_item = {
                date:date,
                total_price:total
            };
            arr.push(profit_item)
        }
        return arr;
    }
    //获取当前月第一天
    function setMonthFirstDay(){
        let date = new Date();
        let year = date.getFullYear();
        date.setDate(1);
        let month = date.getMonth()+1;
        month = month>9?month:'0'+month;
        let day = date.getDate();
        day = day>9?day:'0'+day;
        return `${year}-${month}-${day} 00:00:00`;
    }
    // let default_start = setMonthFirstDay();
    // console.log(default_start);
    $('#start_time').val(profit_start_date);
    $('#end_time').val(profit_end_date);
    getProfitLineChart(profit_start_date,profit_end_date);

    function setP(arg){
        let len = arg.length;
        let p = 0;
        if(len>120){
            p=90;
        }
        else if(len>90){
            p = 75;
        }else if(len>60){
            p = 50;
        }else if(len>30){
            p = 25;
        }
        return p;
    }
    //收益折线图
    function lineChart(profit_date,profit_val){
        var lineChart = echarts.init(document.getElementById("profit_charts"));
        var lineoption = {
            title : {
                text: '商城收益统计',
                textStyle:{
                    fontSize:14
                }
            },
            tooltip : {
                trigger: 'axis'
            },
            legend: {
                data:['单日收益']
            },
            grid:{
                left:50
            },
            calculable : true,
            xAxis : [
                {
                    type : 'category',
                    boundaryGap : false,
                    data : profit_date
                }
            ],
            yAxis : [
                {
                    type : 'value',
                    axisLabel : {
                        formatter: '{value} 元'
                    }
                }
            ],
            dataZoom:[
                {
                    show: true,
                    type:'slider',
                    start: setP(profit_date),
                    end: 100,
                    left:'3%',
                    right:'3%',
                    maxValueSpan: 10,
                },
                {
                    type: 'inside',
                },
            ],
            series : [
                {
                    name:'单日收益',
                    type:'line',
                    data:profit_val,
                    itemStyle:{
                        normal: {
                            label:{
                                show:true,
                                position:'top',
                                textStyle:{
                                    fontSize:14
                                }
                            }
                        }
                    },
                    markPoint : {
                        data : [
                            {type : 'max', name: '最大值'},
                            {type : 'min', name: '最小值'}
                        ]
                    },
                    markLine : {
                        data : [
                            {type : 'average', name: '平均值'}
                        ]
                    }
                }
            ]
        };
        lineChart.setOption(lineoption);
        $(window).resize(lineChart.resize);
    }
















    //获取商品销量排序
    let goodsList = [];
    let saleVolume = [];
    function getSaleVolume(){
        getAsyncAjax(`${global.apiUrl}/get_service_product_xl`,res=>{
            console.log(res);
            for(let i in res){
                var goodsName = res[i].product_name;
                var volume = res[i].all_pay;
                goodsList.push(goodsName);
                saleVolume.push(volume);
            }
        },{
            shop_id: global.community_service_shop_id
        })
    }
    getSaleVolume();

    //echarts
    var barChart = echarts.init(document.getElementById("sale_volume_charts"));
    var baroption = {
        title : {
            text: '商品销量排行',
            textStyle: {
                fontSize: '14'
            }
        },
        tooltip : {
            trigger: 'axis'
        },
        legend: {
            data:[{
                name:'商品销量',
                icon:'circle'
            }]
        },
        grid:{
            left:50
        },
        calculable : true,
        xAxis : [
            {
                type : 'category',
                data : goodsList
            }
        ],
        yAxis : [
            {
                type : 'value'
            }
        ],
        series : [
            {
                name:'商品销量',
                type:'bar',
                data:saleVolume,
                itemStyle:{
                    normal:{
                        // color:'#0b67ff',
                        label:{
                            show:true,
                            position:'top',
                            textStyle:{
                                // color:'#0b67ff',
                                fontSize:16
                            }
                        },

                    }
                },
                barWidth:20,
                label:{
                    show:true,
                    position:'top',
                    // color:'#0b67ff'
                }
                // markPoint : {
                //     data : [
                //         {type : 'max', name: '最大值'},
                //         {type : 'min', name: '最小值'}
                //     ]
                // },
                // markLine : {
                //     data : [
                //         {type : 'average', name: '平均值'}
                //     ]
                // }
            }
        ]
    };
    barChart.setOption(baroption);

    window.onresize = barChart.resize;

})
